//
//  PhotoBrowserViewController.swift
//  SinaWeibo
//
//  Created by NeXTSTEP on 15/10/4.
//  Copyright © 2015年 NeXTSTEP. All rights reserved.
//

import UIKit

class PhotoBrowserViewController: BaseViewController {

    private let WBPhotoBrowserCell = "WBPhotoBrowserCell"
    var urls: [NSURL]
    
    var selectedIndex: Int
    
    
    init(urls array: [NSURL], index: Int) {
        self.urls = array
        self.selectedIndex = index
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    override func viewWillAppear(animated: Bool) {
        
        let indexPath = NSIndexPath(forItem: selectedIndex, inSection: 0)
        collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
    }
    
    override func viewDidAppear(animated: Bool) {
    }
    
    
    
    func save(){
        print(__FUNCTION__)
    }
    
    func close(){
        print(__FUNCTION__)
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    //准备UI
    private func setupUI(){
        view.addSubview(collectionView)
        view.addSubview(closeButton)
        view.addSubview(saveButton)
        //设置子控件
        let rect = CGRect(x: 0, y: UIScreen.mainScreen().bounds.height - 40 , width: 100, height: 32)
        closeButton.frame = CGRectOffset(rect, 8, 0)
        saveButton.frame = CGRectOffset(rect, UIScreen.mainScreen().bounds.width - rect.width - 8 , 0)
        
        //监听方法
        saveButton.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
        closeButton.addTarget(self, action: "close", forControlEvents: UIControlEvents.TouchUpInside)
        
        collectionView.registerClass(PhotoBrowserCell.self, forCellWithReuseIdentifier: WBPhotoBrowserCell)
    }
    
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        
        cv.dataSource = self
        layout.itemSize = self.view.bounds.size
        cv.pagingEnabled = true
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        return cv
    }()
    
    private lazy var closeButton: UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setTitle("关闭", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.backgroundColor = UIColor.darkGrayColor()
        return btn
    }()
    
    private lazy var saveButton: UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setTitle("保存", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.backgroundColor = UIColor.darkGrayColor()
        return btn
    }()
    private var photoScale: CGFloat = 1
}

//注意: extension 中不能定义属性
extension PhotoBrowserViewController : UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return urls.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBPhotoBrowserCell, forIndexPath: indexPath) as! PhotoBrowserCell
        cell.imageURL = urls[indexPath.item]
        return cell
        
    }
    
    func currentImageIndex() -> NSIndexPath {
        return collectionView.indexPathsForVisibleItems().last!
    }
    
    func currentImageView() -> UIImageView {
        let cell = collectionView.cellForItemAtIndexPath(currentImageIndex()) as! PhotoBrowserCell
        return cell.imageView
    }
    
    private func hiddenControls(hidden: Bool) {
        collectionView.backgroundColor = hidden ? UIColor.clearColor() : UIColor.blackColor()
        closeButton.hidden = hidden
        saveButton.hidden = hidden
    }
}





